package List;

public class MySingedLinkedList implements IList{
    static class ListNode{
        int val;
        ListNode next;

        ListNode(int val){
            this.val=val;
        }
    }

    public ListNode head;



    @Override
    public void addFirst(int data) {
        ListNode node=new ListNode(data);
        if(head==null){
            head=node;
        }
        else {
            node.next = head;
            head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node=new ListNode(data);
        ListNode cur=head;
        if(this.head==null){
            this.head=node;
        }
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
    }

    @Override
    public void addIndex(int index, int data) throws PosIllegality{
        ListNode node=new ListNode(data);
        if(index<0||index>size()){
            throw new PosIllegality("获取下标异常！！！");
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==size()){
            addLast(data);
            return;
        }
        ListNode cur=searchPrev(index);
        node.next=cur.next;
        cur.next=node;
    }

    public ListNode searchPrev(int index){
        ListNode cur=head;
        int count=0;
        while(count!=index-1){
            cur=cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur=head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(this.head==null){
            return;
        }
        if(key==this.head.val){
            this.head=head.next;
        }
        ListNode cur=findPrev(key);
        if(cur==null){
            System.out.println("没有你要删除的节点");
        }
        ListNode del=cur.next;
        cur.next=del.next;
    }

    private ListNode findPrev(int key){
        ListNode cur=this.head;
        while(cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if(this.head==null){
            return;
        }
        if(key==this.head.val){
            this.head=head.next;
        }
        ListNode cur=head.next;
        ListNode prev=head;
        while(cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                cur=cur.next;
            }
            else{
                prev=cur;
                cur=cur.next;
            }
        }
    }

    @Override
    public int size() {
        ListNode cur=this.head;
        int count=0;
        while(cur!=null){
            cur=cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur=head;
        while(cur!=null){
            ListNode curNext=cur.next;
            cur.next=null;
            cur=curNext;
        }
        head=null;
    }

    @Override
    public void display() {
        ListNode cur=head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
}
